package org.sadhar.sipp.satuan;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.sadhar.sia.common.ClassConnection;
import org.springframework.jdbc.core.RowMapper;

public class SatuanDAOImpl implements SatuanDAO {

	public SatuanDAOImpl() {
		ClassConnection.getTransactionProxyFactoryBean().setTarget(this);
	}

	@Override
	public List<Satuan> list(String keySearch) {
                List<Satuan> list = new ArrayList<Satuan>();
                List<Map> rows = ClassConnection.getJdbc()
                        .queryForList("SELECT * FROM kamus.satuanBarang WHERE satuan LIKE ?",
                        new Object[]{"%"+keySearch+"%"});
                for(Map m : rows){
                    Satuan satuan = new Satuan();
                    satuan.setKodeSatuanBarang(Integer.valueOf(m.get("kodeSatuanBarang").toString()));
                    satuan.setSatuan(m.get("satuan").toString());
                    list.add(satuan);
                }
                return list;
        }

	@Override
	public Satuan select(int id) {
		String sql = "SELECT * FROM kamus.satuanBarang WHERE kodeSatuanBarang=?";
		return (Satuan) ClassConnection.getJdbc().queryForObject(sql,
				new Object[] { Integer.valueOf(id) }, new RowMapper() {
					public Object mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						return new Satuan(rs.getInt("kodeSatuanBarang"), rs
								.getString("satuan"));
					}
				});
	}

}
